package view;

import java.util.ArrayList;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import dto.Researcher;

public class ResearcherListTableModel extends AbstractTableModel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1103156268989064764L;
	private Vector<Researcher> investigadores = new Vector<Researcher>();
	
	public void addResearcher(Researcher r){
		investigadores.add(r);
		fireTableDataChanged();
	}
	
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return investigadores.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
			case 0:
				return String.valueOf(investigadores.get(rowIndex).getID());
			case 1:
				return investigadores.get(rowIndex);
			case 2:
				if (investigadores.get(rowIndex).getDirector() != null){
					return investigadores.get(rowIndex).getDirector();
				}
				return null;
			case 3:
				if (investigadores.get(rowIndex).getCodirector() != null){
					return investigadores.get(rowIndex).getCodirector();
				}
				return null;
			case 4:
				return investigadores.get(rowIndex).getPapers().size();
			default:
				return new CellPanel(investigadores.get(rowIndex));
		}
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex){
		if (columnIndex == columnNames.length-1){
			return true;
		}
		return false;
	}
	
	private String[] columnNames = new String[]{"ID", "Nombre", "Director", "CoDirector", "Articulos", "Acciones"};
	
	@Override
	public String getColumnName(int columnIndex){
		return columnNames[columnIndex];
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Class getColumnClass(int columnIndex){
		if (columnIndex == 1 || columnIndex == 2 || columnIndex == 3){
			return Researcher.class;
		} else if (columnIndex == 4){
			return Integer.class;
		} else if (columnIndex == columnNames.length-1){
			return CellPanel.class;
		}
		return String.class;
		
	}

	public Researcher getResearcher(int selectedRow, int selectedColumn) {
		if (selectedColumn == 1){
			return investigadores.get(selectedRow);
		} else if (selectedColumn == 2){
			return investigadores.get(selectedRow).getDirector();
		} if (selectedColumn == 3){
			return investigadores.get(selectedRow).getCodirector();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public Vector<Researcher> getResearchers() {
		return (Vector<Researcher>) investigadores.clone();
	}

	public void eliminarInvestigador(Researcher r) {
		investigadores.remove(r);
		fireTableDataChanged();
	}
}